package main

import (
	"entrytask/router"
	"entrytask/router/middleware"
	"entrytask/rpc/config"
	"entrytask/rpc/manager"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"log"
	"os"
	"os/signal"
)

//var pooledClient *rpc.PooledClient

func main() {

	if err := config.Init(); err != nil {
		panic(err)
	}

	// 初始化rpc线程池配资后
	middleware.ConfigRPC()
	//initPooledClient()
	//ConfigRPC()

	// 启动rpc服务端
	server := manager.Registry()
	go server.Run()

	// set gin mode.
	gin.SetMode(viper.GetString("run_mode"))
	// create gin engine => g := gin.New()
	engine := gin.Default()
	engine.Static("/static", "./static")

	router.InitRouter(engine)
	log.Printf("Start to listening the incoming requests on http address: %s\n", viper.GetString("server_addr"))
	//log.Println(http.ListenAndServe(viper.GetString("server_addr"), engine).Error())
	if err := engine.Run(viper.GetString("server_addr")); err != nil {
		log.Fatal("gin engine ListenAndServe:", err)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	for {
		<-c
		log.Println("web app graceful shutdown...")
		return
	}
}

//func initPooledClient() {
//	// 2.使用池化客户端,提升性能
//	pooledClient = rpc.NewPooledClient(viper.GetString("rpc_addr"), 200, 200)
//}

// main_test main for test
/*func main_test()  {
	waitGroup := sync.WaitGroup{}
	waitGroup.Add(2)
	// todo start web server and tcp server
	go func() {
		// wait for logic code run end and count down
		defer waitGroup.Done()

		time.Sleep(time.Second * 2)
		fmt.Println("Entry task wait in goroutine 1!")
	}()

	go func() {
		// wait for logic code run end and count down
		defer waitGroup.Done()

		time.Sleep(time.Second * 3)
		fmt.Println("Entry task wait in goroutine 2!")
	}()

	fmt.Println("Entry task api service start ok!")
	waitGroup.Wait()
}*/
